package com.vitaf.cash.app.databinding

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitaf.cash.app.R
import org.json.JSONObject

class UserTestAct : AppCompatActivity() {
    private var binding: UserDataBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.act_databinding_user)
        binding?.handler = this // 设置点击处理器
        initUserListView()

        val userModel = DataBindingHelper.createModel().apply {
            set("userAvatarUrl","https://pic1.arkoo.com/56D0B40F99F841DF8A2425762AE2565D/picture/o_1i4qop009177v1tgf14db15he1iaj1is.jpg")
            set("userName","testName")
            set("userMobile","88888888")
        }
        binding?.model = userModel
    }

    private fun initUserListView() {
        val userList = arrayListOf<GenericObservableModel>()
        for (i in 0..10){
            userList.add(DataBindingHelper.createModel().apply {
                set("userAvatarUrl","https://img0.baidu.com/it/u=410223476,4138599093&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750")
                set("userName","VitafCash${i}")
                set("userMobile","99999999,${i}")
            })
        }
        val json = JSONObject().apply {
            put("userAvatarUrl","https://pic1.arkoo.com/56D0B40F99F841DF8A2425762AE2565D/picture/o_1i4qop009177v1tgf14db15he1iaj1is.jpg")
            put("userName","hello")
            put("userMobile","333333333")
        }
        userList.add(DataBindingHelper.fromJson(json))

        val userAdapter = UserDataBindingAdapter(userList)
        binding?.userListView?.apply {
            layoutManager = LinearLayoutManager(this@UserTestAct)
            adapter = userAdapter
        }
    }

    // 点击事件方法 - 方法签名必须匹配
    fun onButtonClick(view: View) {
        Toast.makeText(this, "按钮被点击了: ${view.id}", Toast.LENGTH_SHORT).show()
    }

    fun onItemClick(user: GenericObservableModel) {
        Toast.makeText(this, "用户被点击: ${user.getString("userName")}", Toast.LENGTH_SHORT).show()
    }

    fun onImageClick() {
        Toast.makeText(this, "图片被点击", Toast.LENGTH_SHORT).show()
    }
}
